package com.mgm.utils;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.Days;
import org.joda.time.Hours;
import org.joda.time.LocalDate;
import org.joda.time.Seconds;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
 
public class JodaTimeUtil {
 
    public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";
    
//	static {
//		DateTimeZone.setDefault(DateTimeZone.forID("Asia/Shanghai"));
//	}
 
    /**
     * 
     * @Title: getDefZoneCurTime   
     * @Description: TODO(...)   
     * @param: @return      
     * @return: Date      
     * @throws
     */
    public static Date getDefZoneCurTime() {
    	return new DateTime(DateTimeZone.getDefault()).toDate();
    }
    /**
     * 
     * @Title: strToDate   
     * @Description: TODO(...)   
     * @param: @param dateTime
     * @param: @param format
     * @param: @return      
     * @return: Date      
     * @throws
     */
    public static Date strToDate(String dateTime,String format){
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(format);
        DateTime date = dateTimeFormatter.parseDateTime(dateTime);
        return date.toDate();
    }
 
    /**
     * 
     * @Title: dateToStr   
     * @Description: TODO(...)   
     * @param: @param date
     * @param: @param format
     * @param: @return      
     * @return: String      
     * @throws
     */
    public static String dateToStr(Date date,String format){
        if(date == null){
            return StringUtils.EMPTY;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(format);
    }
 
    /**
     * 
     * @Title: strToDate   
     * @Description: TODO(...)   
     * @param: @param dateTime
     * @param: @return      
     * @return: Date      
     * @throws
     */
    public static Date strToDate(String dateTime){
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT);
        DateTime date = dateTimeFormatter.parseDateTime(dateTime);
        return date.toDate();
    }
    
    /**
     * 计算两个时间相差多少天
     * @param startDate
     * @param endDate
     * @return
     */
    public static int diffDay(Date startDate, Date endDate) {
        DateTime dt1 = new DateTime(startDate);
        DateTime dt2 = new DateTime(endDate);
        return Days.daysBetween(dt1, dt2).getDays();
    }
    
    /**
     * 计算两个时间相差多少天
     * @Title: getBetweenDay   
     * @param: @param startTime
     * @param: @param endTime
     * @param: @return      
     * @return: int      
     * @throws
     */
	public static int diffDay(long startTime, long endTime) {
		DateTime startDay = new DateTime(startTime);
		DateTime endDay = new DateTime(endTime);
		return Days.daysBetween(startDay, endDay).getDays();
	}
	
	/**
	 * 两个日期间差多少小时
	 * @Title: diffHour   
	 * @Description: TODO(...)   
	 * @param: @param startDate
	 * @param: @param endDate
	 * @param: @return      
	 * @return: int      
	 * @throws
	 */
	public static int diffHour(Date startDate, Date endDate) {
		DateTime dt1 = new DateTime(startDate);
        DateTime dt2 = new DateTime(endDate);
        return Hours.hoursBetween(dt1, dt2).getHours();
	}
	
	/**
	 * 两个日期间差多少小时
	 * @Title: diffHour   
	 * @Description: TODO(...)   
	 * @param: @param startTime
	 * @param: @param endTime
	 * @param: @return      
	 * @return: int      
	 * @throws
	 */
	public static int diffHour(long startTime, long endTime) {
		DateTime dt1 = new DateTime(startTime);
        DateTime dt2 = new DateTime(endTime);
        return Hours.hoursBetween(dt1, dt2).getHours();
	}
 
	/**
	 * 两个日期间差多少秒
	 * @Title: diffSecond   
	 * @Description: TODO(...)   
	 * @param: @param startDate
	 * @param: @param endDate
	 * @param: @return      
	 * @return: int      
	 * @throws
	 */
	public static int diffSecond(Date startDate, Date endDate) {
		DateTime dt1 = new DateTime(startDate);
        DateTime dt2 = new DateTime(endDate);
        return Seconds.secondsBetween(dt1, dt2).getSeconds();
	}
	
	/**
	 * 两个日期间差多少秒
	 * @Title: diffSecond   
	 * @Description: TODO(...)   
	 * @param: @param startTime
	 * @param: @param endTime
	 * @param: @return      
	 * @return: int      
	 * @throws
	 */
	public static int diffSecond(long startTime, long endTime) {
		DateTime dt1 = new DateTime(startTime);
        DateTime dt2 = new DateTime(endTime);
        return Seconds.secondsBetween(dt1, dt2).getSeconds();
	}
	
	/**
     * 获取指定时间
     * @param year 年
     * @param month 月
     * @param day 天
     * @param hour 小时
     * @param minute 分钟
     * @param seconds 秒
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getPointTime(Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer seconds) {
        return getPointTimePattern(year,month,day,hour,minute,seconds,STANDARD_FORMAT);
    }

    /**
     *
     * @param year 年
     * @param month 月
     * @param day 天
     * @param hour 小时
     * @param minute 分钟
     * @param seconds 秒
     * @param parrten 自定义格式
     * @return parrten
     */
    public static String getPointTimePattern(Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer seconds, String parrten) {
        DateTime dt = new DateTime(year, month, day, hour, minute, seconds);
        String date = dt.toString(parrten);
        return date;
    }

    /**
     * 获取指定日期
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static String getPointDate(Integer year, Integer month, Integer day) {
        return getPointDatParrten(year,month,day,STANDARD_FORMAT);
    }

    /**
     * 获取指定日期 返回指定格式
     * @param year
     * @param month
     * @param day
     * @param parrten
     * @return
     */
    public static String getPointDatParrten(Integer year, Integer month, Integer day, String parrten) {
        LocalDate dt = new LocalDate(year, month, day);
        String date = dt.toString(parrten);
        return date;
    }
	
    public static void main(String[] args) {
    	Date d = getDefZoneCurTime();
    	System.out.println(d);
    	
    	String dateStart = "2013-08-13 16:29:58";
		String dateStop = "2013-08-14 16:29:59";

		Date d1 = strToDate(dateStart);
		Date d2 = strToDate(dateStop);
		System.out.println(diffSecond(d1, d2));
		System.out.println(diffHour(d1, d2));
    }
 
}
